//rory's CS
	var securityDataUrl = 'dataanalysis/stationsecurity';

	lay('#version').html('-v'+ laydate.v);

    //年月范围
    // laydate.render({
    //   elem: '#datepicker',
    //   type: 'month',
    //   range: true
    // });

    //监听获取按钮
    $('#btn-get-security-data').on('click',function(){
    	//获取日期
    	dateInterval=$('#datepicker').val();
    	//获取半径
    	radius=$('#input-radius').val();
    	param={
    		"year":dateInterval.split('-')[0],
    		"month":$.trim(dateInterval.split('-')[1]),
    		"radius":radius
    	};
    	console.log(param);
    	//获取数据
    
		getSecurityData(param);
		
		
    });

    //ajax获取数据
    function getSecurityData(param){
    	$.ajax({
    		url:securityDataUrl,
    		type:'get',
    		data:param,
    		dataType:'json',
    		async:true,
    		success:function(result){
    			data = result.data;
    			//load to table
				
				var dangerIndexMap=new Map();
				var id;
				var stationName;
				
    			var tbody=document.getElementById("table-body");
				
				document.getElementById("security-table").style.display="block";
				
				document.getElementById("security-graph").style.display="block";
    			for (var i=0;i<result.data.length;i++) {
    				var record = data[i];
					
    				var stationBasicInfo = record.stationBasicInfo;
					
    				var tr=document.createElement("tr");
					
					ths=[];
					th=document.createElement("th");
					th.innerHTML=stationBasicInfo.station_id;
					
					id=stationBasicInfo.station_id;
					ths.push(th);
					th=document.createElement("th");
					th.innerHTML=stationBasicInfo.name;
					stationName=stationBasicInfo.name;
					ths.push(th);
					th=document.createElement("th");
					th.innerHTML=record.crashNumber;
					ths.push(th);
					th=document.createElement("th");
					th.innerHTML=record.injuredNumber;
					ths.push(th);	
					th=document.createElement("th");
					th.innerHTML=record.killedNumber;
					ths.push(th);
					th=document.createElement("th");
					th.innerHTML=record.dangerIndex;
					dangerIndexMap.set(id,[record.dangerIndex,stationName]);
					ths.push(th);
					for(var j=0;j<ths.length;j++){
						tr.appendChild(ths[j]);
					}
    				tbody.appendChild(tr);
    			}
    			
			
				var dangerIndexArr=Array.from(dangerIndexMap);
				//排序
				dangerIndexArr.sort(function(a,b){return b[1][0]-a[1][0]});
				//Top 50危险
				var ids=[];
				var dangerIndexes=[];
				var stationNames=[];
				for(i=0;i<dangerIndexArr.length;i++){
					ids.push(dangerIndexArr[i][0]);
					dangerIndexes.push(parseInt(dangerIndexArr[i][1][0]));
					stationNames.push(dangerIndexArr[i][1][1]);
					if(i==49){
						break;
					}
				}
				console.log(ids);
				console.log(dangerIndexes);
				var roryEchartsOpts={
							 title: {
						         text: '站点危险等级Top50'
						     },
						     tooltip: {
						    	 formatter:function(params){
						    		 return stationNames[params.dataIndex];
						    	 }
						     },
						     legend: {
						         data:['危险指数']
						     },
						     xAxis: {
						         data: ids,
						         name:'id'
						     },
						     yAxis: {},
						     series: [{
						         name: '危险指数',
						         type: 'bar',
						         data: dangerIndexes,
						         label: {
						                show: true,
						                position: 'top'
				            	},
				            	itemStyle: {
				            		color:'#A5DBC2'
				            	}
						     }]
						 };
				echarts.init(document.getElementById('rory-echarts')).setOption(roryEchartsOpts);
				console.log("done!");
    		}
    	});
    }
//end

//hkccc's CS

//end

//Asia's CS
	var url_topbikes = 'dataanalysis/getTopBikeAsia';
	var tripduration1;
	var bikeid1;

$.ajax({
	url : url_topbikes,
	type : 'get',
	dataType : 'json',
	async:false,
	success : function(re) {
		jsonData = re.data;
		console.log(jsonData);
		tripduration1 = jsonData.tripduration;
		bikeid1 = jsonData.bikeid;

	},
	error : function(result) {
		alert(result.responseText || 'Some error exist!');
	}
});
//tripduration1[0] = (tripduration1[0]/60).toFixed(2);
//tripduration1[1] = (tripduration1[1]/60).toFixed(2);
//tripduration1[2] = (tripduration1[2]/60).toFixed(2);
//tripduration1[3] = (tripduration1[3]/60).toFixed(2);
//tripduration1[4] = (tripduration1[4]/60).toFixed(2);
						// 基于准备好的dom，初始化echarts实例
				        var myChart = echarts.init(document.getElementById('echart1'));
				        // 指定图表的配置项和数据
						console.log(tripduration1);
						console.log(bikeid1);
						var dataAxis = ['点', '击', '柱', '子', '或', '者', '两', '指', '在', '触', '屏', '上', '滑', '动', '能', '够', '自', '动', '缩', '放'];
						var data = [220, 182, 191, 234, 290, 330, 310, 123, 442, 321, 90, 149, 210, 122, 133, 334, 198, 123, 125, 220];
						var yMax = 500;
						var dataShadow = [];

						for (var i = 0; i < data.length; i++) {
						    dataShadow.push(yMax);
						}

						option = {
							    color: ['#3398DB'],
							    title: {
				        	        text: '正常情况下使用最多的单车',
				        	        left: 'center'
				        	    },
							    tooltip: {
							        trigger: 'axis',
							        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
							            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
							        }
							    },
				        	    legend: {
				        	        orient: 'vertical',
				        	        left: 'right',
				        	        name: 'TripDuration'
				        	    },
							    grid: {
							        left: '3%',
							        right: '4%',
							        bottom: '3%',
							        containLabel: true
							    },
							    xAxis: [
							        {
							            type: 'category',
							            data: bikeid1,
							            axisTick: {
							                alignWithLabel: true
							            }
							        }
							    ],
							    yAxis: [
							        {
							            type: 'value',
							            min : 34500,
							            max : 36000
							        }
							    ],
							    series: [
							        {
							            name: 'TripDuration',
							            type: 'bar',
							            barWidth: '60%',
							            data: tripduration1
							        }
							    ]
							};

				        // 使用刚指定的配置项和数据显示图表。
				        console.log("setoption");
				        myChart.setOption(option);

				    	var url_topbikes2 = 'dataanalysis/getUserType';
				    	var tripduration2;
				    	var usertype2;
				    	//tripduration2[0]=Math.round(tripduration2[0]/3600);
				    		//tripduration2[1]=tripduration2[1]/3600;
				    $.ajax({
				    	url : url_topbikes2,
				    	type : 'get',
				    	dataType : 'json',
				    	async:false,
				    	success : function(re) {
				    		jsonData = re.data;
				    		console.log(jsonData);
				    		tripduration2 = jsonData.tripDur;
				    		usertype2 = jsonData.userType;

				    	},
				    	error : function(result) {
				    		alert(result.responseText || 'Some error exist!');
				    	}
				    });				        
				        
				    tripduration2[0] = (tripduration2[0]/3600).toFixed(2);
				    tripduration2[1] = (tripduration2[1]/3600).toFixed(2);
				    console.log("tripduration2->"+tripduration2);
				        // 基于准备好的dom，初始化echarts实例
				        var myChart2 = echarts.init(document.getElementById('echart2'));
				        // 指定图表的配置项和数据
				        option = {
				        	    title: {
				        	        text: '不同用户类别骑行总时间（单位/小时）',
				        	        left: 'center'
				        	    },
				        	    tooltip: {
				        	        trigger: 'item',
				        	        formatter: '{a} <br/>{b} : {c} ({d}%)'
				        	    },
				        	    legend: {
				        	        orient: 'vertical',
				        	        left: 'left',
				        	        data: usertype2
				        	    },
				        	    series: [
				        	        {
				        	            name: '访问来源',
				        	            type: 'pie',
				        	            radius: '55%',
				        	            center: ['50%', '60%'],
				        	            data: [
				        	                {value: tripduration2[0], name: usertype2[0]},
				        	                {value: tripduration2[1], name: usertype2[1]},
				        	            ],
				        	            emphasis: {
				        	                itemStyle: {
				        	                    shadowBlur: 10,
				        	                    shadowOffsetX: 0,
				        	                    shadowColor: 'rgba(0, 0, 0, 0.5)'
				        	                }
				        	            }
				        	        }
				        	    ]
				        	};

				 
				        // 使用刚指定的配置项和数据显示图表。
				        myChart2.setOption(option);		    
//end

//late-whale's CS

//end
//LRY's CS
console.log("LRY");
var param1 = getQueryString("param1");
var param2 = getQueryString("param2");
if (param1 == null) {
	param1 = 5;
}
if (param2 == null) {
	param2 = 5;
}
var one_url = 'dataanalysis/popular?topStationCountLimit=' + param1
		+ '&lastStationCountLimit=' + param2;
console.log(one_url);
var jsonData;
var topCountXData;
var topCountNameData;
var topCountYData;
var lastCountXData;
var lastCountNameData;
var lastCountYData;

function getQueryString(name) {
	let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	let r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return decodeURIComponent(r[2]);
	}
	return null;
}

// 接收数据
$.ajax({
	url : one_url,
	type : 'post',
	dataType : 'json',
	success : function(result) {
		jsonData = result.data;
		topCountXData = jsonData.topCountXData;
		console.log(topCountXData);
		console.log(jsonData);
		topCountNameData = jsonData.topCountNameData;
		console.log(topCountNameData);
		topCountYData = jsonData.topCountYData;
		console.log(topCountYData);
		lastCountXData = jsonData.lastCountXData;
		console.log(lastCountXData);
		lastCountNameData = jsonData.lastCountNameData;
		console.log(lastCountNameData);
		lastCountYData = jsonData.lastCountYData;
		console.log(lastCountYData);

		// 高频站点
		var topCount = document.getElementById('topCount');
		var topCountChart = echarts.init(topCount);
		var topCountOption = {

			title : {
				text : '高频站点'
			},
			tooltip : {},
			legend : {
				data : [ '出发车次' ]
			},
			xAxis : {
				data : topCountXData
			},
			yAxis : {},
			series : [ {
				name : '出发车次',
				data : topCountYData,
				type : 'bar',
				itemStyle : {
					normal : {
						color : '#91c7ae'
					}
				}
			} ]
		};

		if (topCountOption && typeof topCountOption === "object") {
			topCountChart.setOption(topCountOption);
		}

		// 低频站点
		var lastCount = document.getElementById('lastCount');
		var lastCountChart = echarts.init(lastCount);
		var lastCounlasttion = {

			title : {
				text : '低频站点'
			},
			tooltip : {},
			legend : {
				data : [ '出发车次' ]
			},
			xAxis : {
				data : lastCountXData
			},
			yAxis : {},
			series : [ {
				name : '出发车次',
				data : lastCountYData,
				type : 'bar',
				itemStyle : {
					normal : {
						color : '#61a0a8'
					}
				}

			} ]
		};

		if (lastCounlasttion && typeof lastCounlasttion === "object") {
			lastCountChart.setOption(lastCounlasttion);
		}
	},

	error : function(result) {
		alert(result.responseText || '请求的操作出错了！');
	}
});

// 高频站点车流量
var param = getQueryString("param");
if (param == null) {
	param = 5;
}
var two_url = 'dataanalysis/inout?stationCountLimit=' + param;
console.log(two_url);
var jsonData;
var stationNameXData;
var startStationYData;
var endStationYData;

// 接收数据
$.ajax({
	url : two_url,
	type : 'post',
	dataType : 'json',
	success : function(result) {
		jsonData = result.data;
		stationNameXData = jsonData.stationNameXData;
		console.log(stationNameXData);
		startStationYData = jsonData.startStationYData;
		console.log(startStationYData);
		endStationYData = jsonData.endStationYData;
		console.log(endStationYData);

		// 高频站点车流量
		var stationCount = document.getElementById('stationCount');
		var stationCountChart = echarts.init(stationCount);
		var stationCountOption = {

			tooltip : {
				trigger : 'axis',
				axisPointer : { // 坐标轴指示器，坐标轴触发有效
					type : 'line' // 默认为直线，可选为：'line' | 'shadow'
				}
			},
			legend : {
				data : [ '车辆流出', '车辆流入' ]
			},
			grid : {
				left : '3%',
				right : '4%',
				bottom : '3%',
				containLabel : true
			},
			xAxis : {
				type : 'value'
			},
			yAxis : {
				data : stationNameXData,
			},
			series : [ {
				name : '车辆流出',
				type : 'bar',
				stack : '总量',
				data : startStationYData
			}, {
				name : '车辆流入',
				type : 'bar',
				stack : '总量',
				data : endStationYData
			} ]
		};

		if (stationCountOption && typeof stationCountOption === "object") {
			stationCountChart.setOption(stationCountOption);
		}

	},

	error : function(result) {
		alert(result.responseText || '请求的操作出错了！');
	}
});

// end